<?php

class KosaricaController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        $this->view->render('seznam/_navigation.phtml');
    }

    public function init()
    {
        $this->_helper->layout->setLayout('layout');
              /* Initialize action controller here */
    }

    public function indexAction()
    {
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');
        if ($strankaSession->strankaId) {
            $mapper = new Application_Model_KosaricaMapper();
            //$where = $mapper->getDbTable()->getAdapter()->quoteInto('stranka_emso = ?', $strankaSession->strankaId);

            $select = $mapper->getDbTable()->getAdapter()
                ->select()
                ->from('kosarica')
                ->where('stranka_emso = ?', $strankaSession->strankaId)
                ->where('active = 1');

            $stmt = $mapper->getDbTable()->getAdapter()->query($select);
            $kosaricaDb = $stmt->fetchAll();
            $kosaricaDb = $kosaricaDb[0];

            $strankaSession->kosaricaId = $kosaricaDb['sifra_kosarice'];

            $kosarica = new Application_Model_Kosarica();
            $kosarica->setSifra_kosarice($kosaricaDb['sifra_kosarice']);
            $kosarica->setStranka_emso($kosaricaDb['stranka_emso']);
            $kosarica->setActive($kosaricaDb['active']);
            $kosarica->setTotal($kosaricaDb['total']);

            $elementKosarice = new Application_Model_ElementKosarice();
            $elementKosaricaMapper = new Application_Model_ElementKosariceMapper();

            $where = $elementKosaricaMapper->getDbTable()->getAdapter()->quoteInto('kosarica_sifra_kosarice = ?', $kosaricaDb['sifra_kosarice']);

            $noviElementi = $elementKosaricaMapper->getDbTable()->fetchAll($where);

            $total = 0;

            foreach ($noviElementi as $row)
            {
                $produktMapper = new Application_Model_IzdelkiMapper();
                $izdelek = new Application_Model_Izdelki();

                $produktMapper->find($row['produkt_idprodukt'], $izdelek);

                $total += $row['kolicina']*$izdelek->getCena();
            }
            $kosarica->setTotal($total);
            $mapper->save($kosarica);
        }
    }

    public function dodajAction()
    {
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');
        // poišče košarico za trenutnega uporabnika oz. ustvari novo če še ne obstaja
        if ($strankaSession->strankaId) {

            $mapper = new Application_Model_KosaricaMapper();

            $select = $mapper->getDbTable()->getAdapter()
                ->select()
                ->from('kosarica')
                ->where('stranka_emso = ?', $strankaSession->strankaId)
                ->where('active = 1');

            $stmt = $mapper->getDbTable()->getAdapter()->query($select);
            $kosaricaDb = $stmt->fetchAll();
            $kosaricaDb = $kosaricaDb[0];

            if (!isset($kosaricaDb['sifra_kosarice']) || !$kosaricaDb['sifra_kosarice']) {
                $kosaricaId = $mapper->getLastInsertId()+1;
                $kosarica = new Application_Model_Kosarica();
                $kosarica->setStranka_emso($strankaSession->strankaId);
                $kosarica->setSifra_kosarice($kosaricaId);
                $kosarica->setActive(1);
                $kosarica->setTotal(0);

                $mapper->save($kosarica, true);

                $select = $mapper->getDbTable()->getAdapter()
                    ->select()
                    ->from('kosarica')
                    ->where('stranka_emso = ?', $strankaSession->strankaId)
                    ->where('active = 1');


                $stmt = $mapper->getDbTable()->getAdapter()->query($select);
                $kosaricaDb = $stmt->fetchAll();
                $kosaricaDb = $kosaricaDb[0];

                $strankaSession->kosaricaId = $kosaricaId;
            } else {
                $kosarica = new Application_Model_Kosarica();
                $kosarica->setSifra_kosarice($kosaricaDb['sifra_kosarice']);
                $kosarica->setStranka_emso($kosaricaDb['stranka_emso']);
                $kosarica->setActive($kosaricaDb['active']);
                $kosarica->setTotal($kosaricaDb['total']);
            }

            // dodajanje produkta
            $idProdukt = base64_decode($this->getParam('id'));

            // find produkt
            $elementKosarice = new Application_Model_ElementKosarice();
            $elementKosaricaMapper = new Application_Model_ElementKosariceMapper();

          $where = $elementKosaricaMapper->getDbTable()->getAdapter()->quoteInto('kosarica_sifra_kosarice = ?', $kosaricaDb['sifra_kosarice']);

            $noviElementi = $elementKosaricaMapper->getDbTable()->fetchAll($where);

            $kolicina = 0;

            $insert = true;
        foreach ($noviElementi as $row)
       {
            if ($row["produkt_idprodukt"] == $idProdukt){
                $kolicina=$row["kolicina"];
                $insert = false;
                break;
            }
       }

       $elementKos = new Application_Model_ElementKosarice();
            $elementKos->setKosaricaSifraKosarice($kosaricaDb['sifra_kosarice'])
            ->setKolicina($kolicina+1)
            ->setProdukt_idprodukt($idProdukt);
       $elementKosaricaMapper->save($elementKos, $insert);


        } else {
            $strankaSession->error = "Ta akcija je na voljo samo registriranim strankam!";
            $this->_helper->redirector('index', 'seznam');
        }

        $this->_helper->redirector('index', 'kosarica');
        return;
    }

    public function skupajAction()
    {
        // action body
    }

    public function pregledAction()
    {
        // action body
    }

    public function zakljucekAction()
    {
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');

        $strankaEmso = $strankaSession->strankaId;
        $idkosarice = $strankaSession->kosaricaId;

        if ($strankaEmso && $idkosarice) {
            $narocilo = new Application_Model_Narocilo();
            $narociloMapper = new Application_Model_NarociloMapper();
            $narocilo->setStranka_emso($strankaEmso);
            $narocilo->setKosarica_sifra_kosarice($idkosarice);
            $incrementId = $narociloMapper->getLastInsertId()+1;
            $narocilo->setIdnarocilo($incrementId);


            $kosaricaMapper = new Application_Model_KosaricaMapper();

            $elementKosariceMapper = new Application_Model_ElementKosariceMapper();

            $where = $elementKosariceMapper->getDbTable()->getAdapter()->quoteInto('kosarica_sifra_kosarice = ?', $idkosarice);
            $elementi = $elementKosariceMapper->fetchAll($where);

            $produktMapper = new Application_Model_IzdelkiMapper();

            foreach ($elementi as $element) {
                $produktId = $element['produkt_idprodukt'];
                $produkt = new Application_Model_Izdelki();
                $produktMapper->find($produktId, $produkt);

                if ($produkt->getZaloga()-$element['kolicina'] < 0) {
                    $produktIme = htmlspecialchars($produkt->getIme());
                    $strankaSession->error = "Željena količina produkta $produktIme trenutno ni na voljo.";
                    $this->_helper->redirector('index', 'kosarica');
                    return;
                }

                $produktMapper->update($produktId, "zaloga", $produkt->getZaloga()-$element['kolicina']);
            }

            $narociloMapper->save($narocilo, true);
            $kosaricaMapper->update($idkosarice, "active", 0);


            $strankaSession->incrementId = $incrementId;
        }
    }

    public function odstraniIzdelekAction()
    {
        $idForm = new Application_Form_IdForm();

        if ($idForm->isValid($_POST)) {
            $id = $idForm->getValue('id');
            $elementMapper = new Application_Model_ElementKosariceMapper();

            $elementMapper->delete($id);
            $this->_helper->redirector('index', 'kosarica');

        }

    }

    public function posodobiKolicinoAction()
    {
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');
        if ($strankaSession->strankaId && $strankaSession->kosaricaId) {
            $kolicina = $this->getParam('qty');
            $idprodukt = $this->getParam('idprodukt');

            $mapper = new Application_Model_ElementKosariceMapper();
            $mapper->update(array('sifra_kosarice' => $strankaSession->kosaricaId, 'idprodukt' => $idprodukt), 'kolicina', $kolicina);
        }

        $this->_helper->redirector('index', 'kosarica');
    }


}













